<template>
    <a-drawer
        :destroyOnClose="true"
        placement="right"
        :closable="true"
        :visible="visible"
        :maskClosable="true"
        :width="width"
        :title="title"
        @close="cancel">
        <a-spin :spinning="confirmLoading">
           
            <!-- 这里放自己的组件 -->
            <LForm ref="LForm" @clickEventHandler="clickEventHandler" @changeEventHandler="changeEventHandler" :formAttributes="formAttributes"></LForm>

            <LTable
                ref="fieldList"
                size="small"
                rowKey="keyId"
                :columns="columns"
                axiosUrl="/SysEntityFieldController/list"
                :initQuery="initQuery"
                :initOrder="[{field:'entityField',val:'asc'}]"
                :showPagination="false"
                :showQueryForm="false"
                :isAutoLoad="false">
    
                <span slot="action" slot-scope="text, record">
                    <a @click="handleEdit(record,'entityFieldForm',record.entityDesc)">编辑</a>
                    <a-divider type="vertical"/>
                    <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete({keyId:record.keyId},'/SysEntityFieldController/del','fieldList')">
                            <a>删除</a>
                    </a-popconfirm>
                </span>
                
            </LTable>

            <entityFieldForm ref="entityFieldForm" @close="refresh('fieldList')" @ok="refresh('fieldList')"></entityFieldForm>
           
        </a-spin>
       
        <div style="height:30px;"></div>
        <div class="drawer-bootom-button">
            <a-button shape="round" @click="cancel" style="margin-right: .8rem">取消</a-button>
            <a-button  shape="round" @click="ok" type="primary" >提交</a-button>
        </div>

    </a-drawer>
    
</template>

<script>
import LForm from '@/components/lt/form/LForm'
import pick from 'lodash.pick'
import request from '@/utils/request'
import moment from 'moment'
import LTable from '@/components/lt/LTable'
import {SysMixin} from '@/mixins/SysMixin'
import defaultSettings from '@/config/defaultSettings'
import entityFieldForm from './entityFieldForm'


export default {
    name:'entityForm',
    components:{LForm,LTable,entityFieldForm},
    mixins:[SysMixin],
   
    data(){
        return{
            width:800,
            defaultSettings,
            moment,
            visible:false,
            model:{},
            confirmLoading:false,
            title:'标题',
            formAttributes:{
              type:'',//collapse,tabs
              formItems:[
                {
                  title:'标签一',
                  columns:1,
                  fields:[
                    {field:'keyId',label:'实体内码',component:'input',disabled:true},
                    {field:'systemNo',label:'模块',component:'input',disabled:true,rules: [{ required: false, message: '不能为空!' }],initialValue:defaultSettings.systemNo},
                    {field:'entityClassName',label:'实体名称',component:'inputSearch',rules: [{required: true, message: '不能为空!'}]},
                    {field:'tableName',label:'数据库表名',disabled:true,component:'input',rules: [{required: false, message: '不能为空!'}]},
                    {field:'entityType',label:'实体类型',component:'input',disabled:true},
                    {field:'entityDesc',label:'备注',component:'input'},
                  ]
                },
              ]

            },
            initQuery:[{field:'entityField',val:'asc'}],
            columns:[
                {
                    title: '序号',
                    dataIndex: '',
                    key:'rowIndex',
                    width:60,
                    align:"center",
                    customRender:function (t,r,index) {
                      return parseInt(index)+1;
                    }
                },
                
            //    {
            //         title: '实体名称',
            //         dataIndex: 'entityClassName'
                    
            //     },
                {
                    title: '数据库字段',
                    dataIndex: 'dbField'
                    
                },
                {
                    title: '实体字段代码',
                    dataIndex: 'entityField'
                    
                },
                {
                    title: '实体字段标题',
                    dataIndex: 'entityFieldTitle'
                },
                
                {
                    title: '操作',
                    dataIndex: 'action',
                    width: '120px',
                    scopedSlots: { customRender: 'action' }
                }
            ],
  
        }
    },
    created(){
    },
    computed:{
       
    },
    watch:{
        
    },
    methods:{
        edit (record) {
            this.visible = true
            this.model = Object.assign({}, record)
            this.$nextTick(() => {
               //自己业务
                this.getForm().setFieldsValue(pick(this.model,'keyId','systemNo','tableName','entityClassName','entityDesc','entityType'))
                if(this.model.keyId){
                    this.$refs['fieldList'].setInitQuery([{field:'entityClassName',val:this.model.entityClassName}])
                    this.$refs['fieldList'].searchQuery()
                }
            })
          
        },
       
       
        
        ok(){
            const that = this;
            this.getForm().validateFields((err, values) => {
                if (!err) {
                   
                    that.confirmLoading = true
                    let formData = Object.assign(this.model, values);
                    request({url: '/SysEntityInfoController/save',method: 'post',data: formData}).then((res)=>{
                        if(res.success){
                            that.close()
                            that.$message.success(res.message);
                        }else{
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        
                    })
                    
                }
            })
        },
        cancel(){
            this.visible=false
            this.$emit('handleCancel');
        },
        close () {
            this.$emit('close');
            this.visible = false;
        },
        /**
         * 获取form对象
         */
        getForm(){
            return this.$refs.LForm.form
        },
        changeEventHandler(changeItem){
            this.$emit('changeEventHandler',changeItem);
        },
        clickEventHandler(field,form){
            if("entityClassName"==field){
  
                this.getForm().validateFields((err, values) => {
                    if (!err) {
                        this.confirmLoading = true
                        let formData = Object.assign(this.model, values)
                        request({url: '/SysEntityInfoController/initEntity',method: 'post',data: formData}).then((res)=>{
                            if(res.success){
                                this.close()
                                this.edit(res.result)
                            }else{
                                this.$message.warning(res.message);
                            }
                        }).finally(() => {
                            this.confirmLoading = false;
                        })
                        
                    }
                })
              
            }
        },
    }

}
</script>



<style  scoped>
  .drawer-bootom-button {
    position: absolute;
    z-index: 999999;
    bottom: 0;
    width: 100%;
    /* border-top: 1px solid #e8e8e8; */
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }

</style>